package com.tc.activiti.user.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tc.activiti.base.constants.PageConstants;
import com.tc.activiti.base.constants.StateEnum;
import com.tc.activiti.base.entity.APIRequest;
import com.tc.activiti.base.entity.APIResult;
import com.tc.activiti.user.entity.SysRole;
import com.tc.activiti.user.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zengqingfa
 * @date 2019/10/18 15:40
 * @description
 * @email zengqingfa_java@163.com
 */
@Api(tags = "角色管理", value = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private IRoleService roleService;

    @RequestMapping("/getGroupsByUserName")
    public List<SysRole> getGroupsByUserName(Long userId) {
        return roleService.getGroupsByUserName(userId);
    }

    @ApiOperation(value = "角色分页列表", notes = "角色分页列表")
    @RequestMapping(value = "/getRoleList", method = RequestMethod.GET)
    @ResponseBody
    public APIResult<PageInfo> findByPage(@RequestParam("currentPage") Integer pageNo, @RequestParam Integer pageSize) {
        APIResult<PageInfo> result = new APIResult<>();
        if (pageNo == null) pageNo = PageConstants.DEFAULT_PAGE_NO;
        if (pageSize == null) pageSize = PageConstants.DEFAULT_PAGE_SIZE;
        try {
            //使用分页插件
            PageHelper.startPage(pageNo, pageSize);
            List<SysRole> roleList = roleService.findAll(); //获取后台数据列表
            //根据查询的数据列表，得到分页的结果对象
            PageInfo pageInfo = new PageInfo(roleList);
            result.setData(pageInfo);
            result.setMessage("查询角色成功");
        } catch (Exception e) {
            result.setState(StateEnum.ERROR_PERSON.getCode());
            result.setMessage(StateEnum.ERROR_PERSON.getText());
            result.setCause(e.getMessage());
            logger.error("/role/getRoleList", e);
        }
        return result;
    }

}
